import axios from 'axios';
import { notification } from 'ant-design-vue';

// 本机开发时使用的 token
export const token = '';
const instance = axios.create({
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
  },
});

instance.defaults.baseURL = import.meta.env.VITE_REQUEST_BASE_URL;
// instance.defaults.headers.common['Authorization'] = token;

// request interceptor
instance.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    notification.error({
      message: '提示!',
      description: error.message,
    });
    return Promise.reject(error.message);
  }
);

instance.interceptors.response.use(
  (response) => {
    // 状态码 200，data.code 为 1，请求失败
    if (response.code === 1) {
      notification.error({
        // message: '请求失败！',
        description: response.message || '请求失败！',
      });
      return Promise.reject(response.message);
    }
    // 状态码 200，但是 token 过期
    if (response.code === 2004) {
      notification.error({
        // message: '请求失败！',
        description: response.message || '登录失效！',
      });
      return Promise.reject(response.message);
    }
    return response.data;
  },
  (error) => {
    // 状态码非 200
    notification.error({
      message: '提示!',
      description: error.message,
    });
    return Promise.reject(error.message);
  }
);

export default instance;
